package com.wlcorp.forcazamis.business.system.impl;

import java.util.List;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.business.system.IDepartmentService;
import com.wlcorp.forcazamis.dao.system.IDepartmentDAO;
import com.wlcorp.forcazamis.dao.system.IStaffDAO;
import com.wlcorp.forcazamis.domain.system.Department;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.Validator;

public class DepartmentServiceImpl extends BaseService implements
		IDepartmentService {
	private IDepartmentDAO departmentDAO;
	private IStaffDAO staffDAO;
	public List<Department> getDepartmentList(Department department)  throws ServiceException{
		return departmentDAO.getDepartmentList(department);
	}

	public Department getDepartmentById(Long departmentId)  throws ServiceException{
		if(!Validator.checkValueIsValid(departmentId)){
			return null;
		}
		return departmentDAO.getDepartmentById(departmentId);
	}

	public void saveDepartment(Department department)  throws ServiceException{
		if(null != department.getManagerId()){
			department.setManager(staffDAO.getStaffById(department.getManagerId()));
		}else{
			department.setManager(null);
		}
		departmentDAO.saveDepartment(department);
	}

	public void setDepartmentDAO(IDepartmentDAO departmentDAO)  throws ServiceException{
		this.departmentDAO = departmentDAO;
	}

	public void setStaffDAO(IStaffDAO staffDAO) {
		this.staffDAO = staffDAO;
	}

	public void deleteDepartment(Department department) throws ServiceException {
		Staff staff = new Staff();
		staff.setDepartmentId(department.getDepartmentId());
		Long count = staffDAO.getStaffCount(staff);
		if(count > 0){
			setErrorField(department,AppConstant.BASE_BEAN_STATUS_ERROR,"部门在使用中，无法删除！");
			return;
		}
		departmentDAO.deleteDepartment(department);
	}

	public Department getDepartmentByFlag(Integer flag) throws DaoException {
		return departmentDAO.getDepartmentByFlag(flag);
	}
	

}
